<?php
/*
 * @Author: your name
 * @Date: 2020-06-06 09:13:05
 * @LastEditTime: 2020-06-06 21:14:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hotel\application\api\controller\Order.php
 */
namespace app\api\controller;

use think\Controller;
use think\Db;

class Roomorder extends Base
{
    /**
     * 微信生成订单--房间订单
     */
    public function wxpayRoom()
    {
        //查询用户信息
        $user = db('user')->find($this->user_id);

        //获取参数
        $all = input('post.');

        //检查房间
        $checkRoom = $this->checkWx($all['room_id'], $all['coupon_id']);

        if ($checkRoom['code'] == 0) {
            return json(['code' => 0, 'msg' => $checkRoom['msg']]);
        }

        $result = $checkRoom['data'];

        $data = [
            'order_number' => makeOrderNo(),
            'user_id' => $this->user_id,
            'room_id' => $all['room_id'],
            'in_time' => $all['in_time'], //入驻时间
            'out_time' => $all['out_time'], //离店时间
            'go_time' => $all['go_time'], //到店时间
            'days' => $all['days'], //入驻天数
            'money' => $result['need_money'],
            'ordermoney' => $result['new_price'],
            'addtime' => time(),
            'status' => 0,
            'second_card_id' => 0,
            'coupon_id' => $all['coupon_id'], //优惠券id
            'type' => 0, //支付方式微信
            'phone' => $all['phone'], //联系电话
            'idcard' => $user['idcard'], //身份证号码
            'sex' => $user['sex'], //性别
            'realname' => $all['realname'], //真实姓名
            'address' => $all['address'], //地址
            'invoice_id' => $all['invoice_id'], //发票id
            'remark' => $all['remark'], //备注
            'room_num' => $all['room_num'], //人数
        ];

        $res = db('order')->insert($data);
        if ($res) {
            return json(['code' => 1, 'msg' => "订单生成成功", 'data' => $data['order_number']]);
        } else {
            return json(['code' => 1, 'msg' => "订单生成失败", 'data' => $data['order_number']]);
        }
    }
    
    //微信支付检查参数
    public function checkWx($room_id, $coupon_id)
    {
        
        //查询用户信息
        $user = db('user')->find($this->user_id);

        //根据用户的等级查找折扣
        $percent = db('vip_cate')->where(['level' => $user['vip_id']])->value('percent');

        $money = 0;

        $H = date('H');
        $time = time();
        //查找酒店设置的钟点房的信息
        $hotel = db('hotel')->field('start_time,end_time')->find(1);

        $room = db('room')
            ->where(['id' => $room_id, 'status' => 0])
            ->field('one_cate,new_price,is_coupon,is_second,is_recharge,room_num')
            ->find();

        $money = sprintf("%.2f", $room['new_price'] * $percent) / 10;

        if (empty($room)) {
            return ['code' => 0, 'msg' => "房间已下架"];
        }

        if ($room['room_num'] != -1 && $room['room_num'] < 1) {

            return ['code' => 0, 'msg' => "房间已被预约满了"];
        }

        if ($room['one_cate'] == 1) { //钟点房
            if ($hotel['start_time'] > $H || $hotel['end_time'] < $H) {

                return ['code' => 0, 'msg' => "房间已被预约满了"];
            }
        }

        if ($coupon_id) {
            if ($room['is_coupon'] == 1) {
                return ['code' => 0, 'msg' => "该房间不支持优惠券"];
            }
        }

        //查找优惠券
        $coupon = db('coupon_user')
            ->where(['user_id' => $this->user_id, 'id' => $coupon_id, 'status' => 0])
            ->where("end_time>'{$time}'")
            ->find();

        if (empty($coupon)) {
            return ['code' => 0, 'msg' => "优惠券失效"];
        } else {
            $coupon_info = unserialize($coupon['coupon_info']);
            //满多少可用
            $full_money = $coupon_info['full_money'];

            if ($full_money > $money) {
                return ['code' => 0, 'msg' => "未达到优惠券使用规则"];
            }

            // //支付金额
            $money = $money - $coupon_info['money'];
            if ($money <= 0) {
                return ['code' => 0, 'msg' => "未达到优惠券使用规则"];
            }
        }

        $room['need_money'] = $money;
        return ['code' => 1, 'msg' => "可以预约", 'data' => $room];
    }
    
    
    /**
     * 次卡生成订单--房间订单---不考虑优惠券和打折
     */
    public function secondpayRoom()
    {

        //查询用户信息
        $user = db('user')->find($this->user_id);

        //获取参数
        $all = input('post.');
        $card_id = $all['card_id'];
        $card = db('second_card_user')->where(['user_id' => $this->user_id, 'id' => $card_id])->find();

        if ($card['status'] == 2) {
            return json(['code' => 0, 'msg' => "次卡已过期"]);
        }

        if ($card['endtime'] < time()) {
            return json(['code' => 0, 'msg' => "次卡已过期"]);
        }

        if ($card['card_num'] < 0) {
            return json(['code' => 0, 'msg' => "次卡次数不足"]);
        }
        //检查参数
        $checkSecond = $this->checkSecond($all['room_id']);
        
        if ($checkSecond['code'] == 0) {
            return json(['code' => 0, 'msg' => $checkSecond['msg']]);
        }

        $result = $checkSecond['data'];

        $data = [
            'order_number' => makeOrderNo(),
            'user_id' => $this->user_id,
            'room_id' => $all['room_id'],
            'in_time' => $all['in_time'], //入驻时间
            'out_time' => $all['out_time'], //离店时间
            'go_time' => $all['go_time'], //到店时间
            'days' => $all['days'], //入驻天数
            'money' => $result['new_price'],
            'ordermoney' => $result['new_price'],
            'addtime' => time(),
            'status' => 1,
            'second_card_id' => $all['card_id'],
            'coupon_id' => 0, //优惠券id
            'type' => 1, //支付方式次卡
            'phone' => $all['phone'], //联系电话
            'idcard' => $user['idcard'], //身份证号码
            'sex' => $user['sex'], //性别
            'realname' => $all['realname'], //真实姓名
            'address' => $all['address'], //地址
            'invoice_id' => $all['invoice_id'], //发票id
            'remark' => $all['remark'], //备注
            'room_num' => $all['room_num'], //人数
        ];
        //减去用户的次卡次数
        db('second_card_user')->where(['user_id' => $this->user_id, 'id' => $card_id])->setDec('card_num', 1);
        db('second_card_user')->where(['user_id' => $this->user_id, 'id' => $card_id])->setInc('use_num', 1);
        $res = db('order')->insert($data);
        if ($res) {
            return json(['code' => 1, 'msg' => "订单生成成功", 'data' => $data['order_number']]);
        } else {
            return json(['code' => 1, 'msg' => "订单生成失败", 'data' => $data['order_number']]);
        }
    }
    //次卡检查参数
    public function checkSecond($room_id)
    {

        $H = date('H');
        $time = time();
        //查找酒店设置的钟点房的信息
        $hotel = db('hotel')->field('start_time,end_time')->find(1);

        $room = db('room')
            ->where(['id' => $room_id, 'status' => 0])
            ->field('one_cate,new_price,is_second,room_num')
            ->find();

        if (empty($room)) {
            return ['code' => 0, 'msg' => "房间已下架"];
        }

        if ($room['is_second'] == 1) {
            return ['code' => 0, 'msg' => "该房间不支持次卡支付"];
        }

        if ($room['room_num'] != -1 && $room['room_num'] < 1) {

            return ['code' => 0, 'msg' => "房间已被预约满了"];
        }

        if ($room['one_cate'] == 1) { //钟点房
            if ($hotel['start_time'] > $H || $hotel['end_time'] < $H) {

                return ['code' => 0, 'msg' => "房间已被预约满了"];
            }
        }

        return ['code' => 1, 'msg' => "可以预约", 'data' => $room];
    }
    /**
     * 余额生成订单--房间订单
     */
    public function moneypayRoom()
    {
        //查询用户信息
        $user = db('user')->find($this->user_id);

        //获取参数
        $all = input('post.');

        //检查房间
        $checkMoney = $this->checkMoney($all['room_id']);

        if ($checkMoney['code'] == 0) {
            return json(['code' => 0, 'msg' => $checkMoney['msg']]);
        }

        $result = $checkMoney['data'];

        $data = [
            'order_number' => makeOrderNo(),
            'user_id' => $this->user_id,
            'room_id' => $all['room_id'],
            'in_time' => $all['in_time'], //入驻时间
            'out_time' => $all['out_time'], //离店时间
            'go_time' => $all['go_time'], //到店时间
            'days' => $all['days'], //入驻天数
            'money' => $result['new_price'],
            'ordermoney' => $result['new_price'],
            'addtime' => time(),
            'status' => 1,
            'second_card_id' => 0,
            'coupon_id' => 0, //优惠券id
            'type' => 2, //支付方式余额
            'phone' => $all['phone'], //联系电话
            'idcard' => $user['idcard'], //身份证号码
            'sex' => $user['sex'], //性别
            'realname' => $all['realname'], //真实姓名
            'address' => $all['address'], //地址
            'invoice_id' => $all['invoice_id'], //发票id
            'remark' => $all['remark'], //备注
            'room_num' => $all['room_num'], //人数
        ];

        $res = db('order')->insert($data);
        if ($res) {
            return json(['code' => 1, 'msg' => "订单生成成功", 'data' => $data['order_number']]);
        } else {
            return json(['code' => 1, 'msg' => "订单生成失败", 'data' => $data['order_number']]);
        }

    }
    //余额检查参数
    public function checkMoney($room_id)
    {

        $H = date('H');
        $time = time();
        //查找酒店设置的钟点房的信息
        $hotel = db('hotel')->field('start_time,end_time')->find(1);

        $room = db('room')
            ->where(['id' => $room_id, 'status' => 0])
            ->field('one_cate,new_price,is_recharge,room_num')
            ->find();

        if (empty($room)) {
            return ['code' => 0, 'msg' => "房间已下架"];
        }

        if ($room['is_recharge'] == 1) {
            return ['code' => 0, 'msg' => "该房间不支持余额支付"];
        }

        if ($room['room_num'] != -1 && $room['room_num'] < 1) {

            return ['code' => 0, 'msg' => "房间已被预约满了"];
        }

        if ($room['one_cate'] == 1) { //钟点房
            if ($hotel['start_time'] > $H || $hotel['end_time'] < $H) {

                return ['code' => 0, 'msg' => "房间已被预约满了"];
            }
        }

        //检查用户余额
        $money = db('user')->where(['user_id'=>$this->user_id])->field('money')->find();
        if($money<$room['new_price']){
            return ['code' => 0, 'msg' => "余额不足"];
        }
        return ['code' => 1, 'msg' => "可以预约", 'data' => $room];
    }
}
